package com.zhang.util;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

/**
 * 描述:获取es连接
 *
 * @author Zhao
 * @create 2022-04-14 2:34
 */
public class GetEsConnClient {
    private static final String HOST = "47.119.136.169";
    private static final int PORT = 9200;
    private static final String userName= "elastic";
    private static final String password= "elastic";
    private static final RestHighLevelClient client;
    static {

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(userName, password));

        client = new RestHighLevelClient(
            RestClient
                .builder(new HttpHost(HOST, PORT, "http")));
                // .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                //     @Override
                //     public HttpAsyncClientBuilder customizeHttpClient(
                //         HttpAsyncClientBuilder httpClientBuilder) {
                //         httpClientBuilder.disableAuthCaching();
                //         return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                //     }
                // }));
    }
    public static RestHighLevelClient getClient(){
        return client;
    }
}
